package com.expressage.controller;
import com.expressage.po.CityAddress;
import com.expressage.po.Role;
import com.expressage.po.User;
import com.expressage.service.*;
import com.expressage.vo.AreaAddressVO;
import com.expressage.vo.StreetAddressVO;
import com.expressage.vo.UserVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户控制器
 */

@Controller
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    StaffService staffService;
    @Autowired
    CheckRecordService checkRecordService;
    @Autowired
    PackageService packageService;
    @Autowired
    SendService sendService;
    @Autowired
    SiteService siteService;
    @Autowired
    RoleService roleService;
    @Autowired
    CityAddressService cityAddressService;
    @Autowired
    StreetAddressService streetAddressService;
    @Autowired
    AreaAddressService areaAddressService;
    @Autowired
    SalaryService salaryService;

    //创建日志
    Logger logger = Logger.getLogger(UserController.class);

    @RequestMapping("/tologin")
    public String toLogin(){
        return "jspbefore/login";
    }

    @RequestMapping("login")
    public String login(User user, HttpSession session, HttpServletRequest request) {
        //logger.info("验证码："+captcha);
        //判断用户的验证码是否输入正确
        //String captchaSession=session.getAttribute("simpleCaptcha").toString();
        User currentUser = userService.findByTelAndPass(user);
        if (currentUser != null) {
            session.setAttribute("currentUser", currentUser);
            return "jspafter/main";
        } else {
            request.setAttribute("mess", "用户名或密码错误！");
            return "jspbefore/login";
        }
    }

    @RequestMapping("/toregist")
        public String toRegist(Model model){
        logger.info("进入去用户注册控制器");
        List<Role> list1 = roleService.findAll();
        model.addAttribute("role",list1);
        List<CityAddress> list2=cityAddressService.findAll();
        model.addAttribute("city",list2);
        List<AreaAddressVO> list3 =areaAddressService.findAll();
        model.addAttribute("area",list3);
        List<StreetAddressVO>list4 =streetAddressService.findAll();
        model.addAttribute("street",list4);
        return "jspbefore/regist";
    }

    @RequestMapping("/regist")
    public String Regist(User user){
        logger.info("进入用户注册控制器");
        boolean flag=userService.addUser(user);
        logger.info(flag);
        return "redirect:tologin.action";
    }



    @RequestMapping("/exit")
    public String Exit(HttpSession session){
        logger.info("进入退出控制器");
        session.invalidate();//清空Session
        return "redirect:index.action";
    }

    @RequestMapping("/userlist")
    public String userlist(Model model){
        List<UserVO> list=userService.findAll();
        logger.info("进入了人员列表控制器");
        model.addAttribute("list",list);
        return "jspafter/userlist";
    }

    @RequestMapping("/toadduser")
    public String toadduser(Model model){
        List<Role> list1 = roleService.findAll();
        model.addAttribute("role",list1);
        List<CityAddress> list2=cityAddressService.findAll();
        model.addAttribute("city",list2);
        List<AreaAddressVO> list3 =areaAddressService.findAll();
        model.addAttribute("area",list3);
        List<StreetAddressVO>list4 =streetAddressService.findAll();
        model.addAttribute("street",list4);
        return "jspafter/adduser";
    }

    @RequestMapping("adduser")
    public String adduser(User user){
        logger.info(user);
        boolean flag=userService.addUser(user);
        logger.info(flag);
        return "redirect:userlist.action";
    }

    @RequestMapping("/deleteuser")
    public String deleteuser(int[] id){
        boolean flag=userService.deleteUser(id);
        logger.info(flag?"删除人员成功":"删除人员失败");
        return "redirect:userlist.action";
    }

    @RequestMapping("/toupdateuser")
    public String toUpdateUser(int uid ,Model model){
        User user=userService.findById(uid);
        model.addAttribute("user",user);
        logger.info("进入去修改用户信息控制器");
        return "jspafter/updateuser";
    }

    @RequestMapping("/updateuser")
    public String updateUser(User user){
        boolean flag= userService.updateUser(user);
        logger.info(flag);
        return "redirect:userlist.action";
    }
}